//Promise可以解决回调地狱，转为.then的链式调用

//引入fs模块
import fs from "fs";
import { resolve } from "path";

//封装读取文件函数
const myReadFile = (fpath) => {
  const p = new Promise((resolve, reject) => {
    fs.readFile(fpath, "utf-8", (err, dataStr) => {
      !err ? resolve(dataStr) : reject(err);
    });
  });
  return p;
};
myReadFile("./text/1.txt")
  .then((res) => {
    console.log(res);
    return myReadFile("./text/2.txt");
  })
  .then((res) => {
    console.log(res);
    return myReadFile("./text/3.txt");
  })
  .then((res) => {
    console.log(res);
    return myReadFile("./text/4.txt");
  })
  .then((res) => {
    console.log(res);
    return myReadFile("./text/5.txt");
  })
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });
